﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Flight_Reservation.Algrithm
{
    class ReachableNodesList
    {
        public List<ReachableNode> ReachableNodes { get; private set; }
        public List<Node> Nodes { get; private set; }
        private Dictionary<Node, ReachableNode> rNodeDictionary;

        public ReachableNodesList()
        {
            ReachableNodes = new List<ReachableNode>();
            Nodes = new List<Node>();
            rNodeDictionary = new Dictionary<Node, ReachableNode>();
        }

        public ReachableNode GetReachableFromNode(Node node)
        {
            return rNodeDictionary.ContainsKey(node) ? rNodeDictionary[node] : null;
        }

        public void AddReachableNode(ReachableNode rNode)
        {
            if (Nodes.Contains(rNode.Node))
            {
                ReachableNode oldReachableNode = GetReachableFromNode(rNode.Node);
                if (rNode.Edge.Price < oldReachableNode.Edge.Price)
                {
                    oldReachableNode.Edge = rNode.Edge;//Makes no sense
                }
            }
            else
            {
                ReachableNodes.Add(rNode);
                Nodes.Add(rNode.Node);
                rNodeDictionary.Add(rNode.Node, rNode);
            } //HVOR SKAL ELSE VÆRE???????????????????????????????
        }

        public void RemoveReachableNode(ReachableNode rNode)
        {
            ReachableNodes.Remove(rNode);
            Nodes.Remove(rNode.Node);
        }

        public bool HasNode(Node node)
        {
            return Nodes.Contains(node);
        }

        public void Reset()
        {
            ReachableNodes = new List<ReachableNode>();
            Nodes = new List<Node>();
            rNodeDictionary = new Dictionary<Node, ReachableNode>();
        }
    }
}
